import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QGridLayout
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QPixmap
from qfluentwidgets import SmoothScrollArea


class HomeInterface(SmoothScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.view = QWidget(self)
        self.vBoxLayout = QVBoxLayout(self.view)
        self.setWidget(self.view)
        self.setWidgetResizable(True)

        # 将背景样式直接应用到主视图，修改背景图片路径
        self.view.setStyleSheet('''
            QWidget#mainView {
                border-image: url(pic/background.png) 0 0 0 0 stretch stretch;
            }
        ''')
        self.view.setObjectName("mainView")

        # 确保滚动区域和其他组件保持透明
        self.setStyleSheet('''
            QScrollArea {
                border: none;
                background: transparent;
            }
            QLabel {
                background: transparent;
            }
            QPushButton {
                border: none;
                background-color: transparent;
            }
        ''')

        self.parent_window = parent  # 保存父窗口引用
        self.initUI()

    def initUI(self):
        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.setSpacing(20)  # 垂直间距

        # 创建网格布局用于放置按钮
        grid_layout = QGridLayout()
        grid_layout.setSpacing(60)  # 增加按钮间距到60像素

        # 创建按钮容器并设置透明背景
        button_container = QWidget()
        button_container.setStyleSheet('''
            QWidget {
                background: transparent;
                border: none;
            }
        ''')
        button_container.setLayout(grid_layout)

        # 创建按钮和设置背景图片
        buttons = [
            ('button1.png', 'dataGeneratorInterface'),
            ('button2.png', 'mathInterface'),
            ('button3.png', 'planningInterface'),
            ('button4.png', 'animationInterface'),
            ('button5.png', 'batchInterface'),
            ('button6.png', 'statsInterface')
        ]

        # 在网格中添加按钮
        for index, (img, target) in enumerate(buttons):
            button = HoverMoveButton(self.view)
            # 修改图片路径
            pixmap = QPixmap(f"pic/{img}")
            button.setFixedSize(pixmap.size())
            # 修改样式表中的图片路径
            button.setStyleSheet(f'''
                QPushButton {{
                    border: none;
                    background-image: url(pic/{img});
                    background-position: center;
                    background-repeat: no-repeat;
                }}
            ''')

            # 设置按钮点击事件
            button.clicked.connect(lambda checked, t=target: self.switch_interface(t))

            # 计算按钮位置（每行3个）
            row = index // 3
            col = index % 3
            grid_layout.addWidget(button, row, col, Qt.AlignCenter)

        # 添加弹性空间将按钮推到底部
        self.vBoxLayout.addStretch()
        self.vBoxLayout.addWidget(button_container, alignment=Qt.AlignCenter)
        self.vBoxLayout.addSpacing(50)  # 底部留出一些空间

    def switch_interface(self, interface_name):
        """切换到指定界面"""
        if self.parent_window:
            # 切换导航栏项目
            self.parent_window.navigationInterface.setCurrentItem(interface_name)
            # 切换到对应的界面
            self.parent_window.stackedWidget.setCurrentWidget(getattr(self.parent_window, interface_name))


class HoverMoveButton(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.original_pos = self.pos()
        self.move_offset = QPoint(5, 5)  # 往左下移动的偏移量

    def enterEvent(self, event):
        self.original_pos = self.pos()
        self.move(self.pos() + self.move_offset)
        super().enterEvent(event)

    def leaveEvent(self, event):
        self.move(self.original_pos)
        super().leaveEvent(event)